package com.zxm.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zxm.edu.dao.ArticleDao;
import com.zxm.edu.dto.CategoryBackDTO;
import com.zxm.edu.dto.CategoryDTO;
import com.zxm.edu.dto.CategoryOptionDTO;
import com.zxm.edu.util.BeanCopyUtils;
import com.zxm.edu.util.PageUtils;
import com.zxm.edu.vo.ConditionVO;
import com.zxm.edu.vo.PageResult;
import com.zxm.edu.entity.Article;
import com.zxm.edu.entity.Category;
import com.zxm.edu.dao.CategoryDao;
import com.zxm.edu.exception.BizException;
import com.zxm.edu.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxm.edu.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
 * 分类服务
 *
 * @author silencezheng
 * @date 2021/07/29
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
	@Autowired
	private CategoryDao categoryDao;
	@Autowired
	private ArticleDao articleDao;

	@Override
	public PageResult<CategoryDTO> listCategories() {
		return new PageResult<>(categoryDao.listCategoryDTO(), categoryDao.selectCount(null));
	}

	@Override
	public PageResult<CategoryBackDTO> listBackCategories(ConditionVO condition) {
		// 查询分类数量
		Integer count = categoryDao.selectCount(new LambdaQueryWrapper<Category>()
				.like(StringUtils.isNotBlank(condition.getKeywords()), Category::getCategoryName, condition.getKeywords())
				.eq(condition.getType()!=null,Category::getType,condition.getType()));
		if (count == 0) {
			return new PageResult<>();
		}
		// 分页查询分类列表
		List<CategoryBackDTO> categoryList = categoryDao.listCategoryBackDTO(PageUtils.getLimitCurrent(), PageUtils.getSize(), condition);
		return new PageResult<>(categoryList, count);
	}

	@Override
	public List<CategoryOptionDTO> listCategoriesBySearch(ConditionVO condition) {
		// 搜索分类
		List<Category> categoryList = categoryDao.selectList(new LambdaQueryWrapper<Category>()
				.like(StringUtils.isNotBlank(condition.getKeywords()), Category::getCategoryName, condition.getKeywords())
						.eq(condition.getType()!=null,Category::getType,condition.getType())
				.orderByDesc(Category::getId));
		return BeanCopyUtils.copyList(categoryList, CategoryOptionDTO.class);
	}

	@Override
	public void deleteCategory(List<Integer> categoryIdList) {
		// 查询分类id下是否有资讯
		Integer count = articleDao.selectCount(new LambdaQueryWrapper<Article>()
				.in(Article::getCategoryId, categoryIdList));
		if (count > 0) {
			throw new BizException("删除失败，该分类下存在资讯/课程");
		}
		categoryDao.deleteBatchIds(categoryIdList);
	}

	@Override
	public void saveOrUpdateCategory(CategoryVO categoryVO) {
		// 判断分类名重复
		Category existCategory = categoryDao.selectOne(new LambdaQueryWrapper<Category>()
				.select(Category::getId)
				.eq(Category::getCategoryName, categoryVO.getCategoryName()));
		if (Objects.nonNull(existCategory) && !existCategory.getId().equals(categoryVO.getId())) {
			throw new BizException("分类名已存在");
		}
		Category category = Category.builder()
				.id(categoryVO.getId())
				.categoryName(categoryVO.getCategoryName())
				.type(categoryVO.getType())
				.build();
		this.saveOrUpdate(category);
	}

}
